package cn.fintecher.pangolin.service.business.model.request;

import cn.fintecher.pangolin.common.enums.FollowType;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Objects;

/**
 * @Auther: xiaqun
 * @Description: 查询催记请求参数
 * @Date: 18:25 2019/9/16
 */

@Data
public class FindCaseFollowRecordRequest {
    @ApiModelProperty(value = "证件号")
    private String certificateNo;

    @ApiModelProperty(notes = "跟进方式")
    private FollowType type;

    @ApiModelProperty(notes = "跟进时间开始")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date minFollowTime;

    @ApiModelProperty(notes = "跟进时间结束")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date maxFollowTime;

    public BoolQueryBuilder getBuilder() {
        BoolQueryBuilder builder = new BoolQueryBuilder();
        builder.must(QueryBuilders.matchPhraseQuery("certificateNo.keyword", this.certificateNo));
        builder.must(QueryBuilders.matchPhraseQuery("collectionWay", 1));
        if (Objects.nonNull(this.type)) {
            builder.must(QueryBuilders.matchPhraseQuery("type.keyword", this.type.name()));
        }
        if (Objects.nonNull(this.minFollowTime)) {
            builder.must(QueryBuilders.rangeQuery("operateTime").gte(this.minFollowTime.getTime()));
        }
        if (Objects.nonNull(this.maxFollowTime)) {
            builder.must(QueryBuilders.rangeQuery("operateTime").lte(this.maxFollowTime.getTime() + 86400000));
        }
        return builder;
    }
}